<template>
    <div class="search">

        <mu-appbar :title="title">
            <mu-icon-button @click="$router.back(-1);" icon="navigate_before" slot="left"/>
            <mu-icon-button slot="right"/>
        </mu-appbar>

        <div class="searbar">
            <mu-row gutter>
                <mu-col width="90" tablet="90" desktop="90">
                    <mu-text-field hintText="输入关键字搜索课程" v-model="keyword" fullWidth/>
                </mu-col>
                <mu-col width="10" tablet="10" desktop="10">
                    <mu-icon-button class="search-btn" @click="search" icon="search"/>
                </mu-col>
            </mu-row>
        </div>

        <div class="search-result">
            <component :is="showcourse" :courses="searchCourses"></component>
        </div>

    </div>
</template>

<script>

  import CourseList from './CourseList'

  export default {
    name: 'Search',
    created() {
      this.$store.dispatch('setMessage', '输入关键字搜索课程')
      this.keyword = this.$store.state.keyword;
      console.log(this.$route);
      if( this.$route.params.yxid ){
        if(  this.$route.params.xsm != '' ){
          this.title = this.$route.params.xsm;
        }
        this.$store.dispatch('setMessage', '课程加载中，请稍候...')
        this.$store.dispatch('searchCourses', {yxid:this.$route.params.yxid});
      }
    },
    computed: {
      searchCourses() {
        return this.$store.state.searchCourses;
      }
    },
    data() {
      return {
        keyword: '',
        showcourse: 'list',
        title:'搜索',
      }
    },
    methods: {
      search() {
        if (this.keyword != '') {
          this.$store.dispatch('setMessage', '课程加载中，请稍候...')
          this.$store.dispatch('searchCourses', {keyword:this.keyword,yxid:this.$route.params.yxid});
        } else {
          this.$store.dispatch('setMessage', '请先输入要搜索的关键字')
        }
      }
    },
    components: {
      list: CourseList
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
    .searbar {
        width: 100%;
        padding: 10px 1rem 0 1rem;
        position: fixed;
        top: 56px;
        left: 0px;
        z-index: 100;
        background: white;
        height: 50px;
        overflow: hidden;;
    }

    .search-btn {
        margin-top: -6px;
        color: #999;
        margin-left: -12px;
    }

    .search-result {
        height: 100%;
        margin-top: 100px;
    }

</style>
